﻿using CRM.Admin.Contracts.Contract.Customer;
using CRM.Admin.Contracts.Model.Customer;
using CRM.Admin.Contracts.Dto.Customer;
using KingsSharp.Core;
using KingsSharp.Core.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using KingsSharp.Common;
using KingsSharp.Common.Data;

namespace CRM.Admin.Services.Service.Customer
{
    public class NatureService : ServiceBase,INatureService
    {
        private IRepository<Nature, Guid> natureRepository;
        private IRepository<NatureCatalog, Guid> natureCatalogRepository;
        public NatureService(IRepository<Nature, Guid> _natureRepository,
            IRepository<NatureCatalog, Guid> _natureCatalogRepository)
            : base(_natureRepository.UnitOfWork)
        {
            natureRepository = _natureRepository;
            natureCatalogRepository = _natureCatalogRepository;
        }
        public IQueryable<NatureCatalog> NatureCatalogs { get { return natureCatalogRepository.Entities; } }

        public IQueryable<Nature> Natures {get {return  natureRepository.Entities;}}

        public OperationResult Insert(params NatureCatalogDto[] dtos)
        {
            dtos.CheckNotNull("dtos");
            foreach (var t in dtos)
            {
                t.Id = Guid.NewGuid();
            }
            var result = natureCatalogRepository.Insert(dtos);
            return result;
        }

        public OperationResult Insert(params NatureDto[] dtos)
        {
            dtos.CheckNotNull("dtos");
            foreach (var t in dtos)
            {
                t.Id = Guid.NewGuid();
            }
            var result = natureRepository.Insert(dtos);
            return result;
        }

        public OperationResult Update(params NatureCatalogDto[] dtos)
        {
            dtos.CheckNotNull("dtos");
            var result = natureCatalogRepository.Update(dtos);
            return result;
        }

        public OperationResult Update(params NatureDto[] dtos)
        {
            dtos.CheckNotNull("dtos");
            var result = natureRepository.Update(dtos);
            return result;
        }

        public OperationResult Delete(bool isNature = false, params Guid[] ids)
        {
            ids.CheckNotNull("ids");
            if (isNature)
            {
                var entitys = natureRepository.Entities.Where(a => ids.Contains(a.Id)).ToList();
                entitys.ForEach(a => a.IsDeleted = true);
                int result = natureRepository.Update(entitys.ToArray());
                return new KingsSharp.Common.Data.OperationResult(KingsSharp.Common.Data.OperationResultType.Success, "删除成功");
            }
            else
            {
                var entitys = natureCatalogRepository.Entities.Where(a => ids.Contains(a.Id)).ToList();
                entitys.ForEach(a => a.IsDeleted = true);
                int result = natureCatalogRepository.Update(entitys.ToArray());
                return new KingsSharp.Common.Data.OperationResult(KingsSharp.Common.Data.OperationResultType.Success, "删除成功");
            }
        }
    }
}
